package com.haliyoo.adhere.controller;

import java.util.List;

import weibo4android.Status;
import weibo4android.Weibo;
import weibo4android.WeiboException;

import android.content.Context;
import android.view.View;
import android.widget.Toast;

import com.haliyoo.adhere.R;
import com.haliyoo.adhere.activity.CreativeDesignActivity;
import com.haliyoo.adhere.bean.WeiboBean;
import com.haliyoo.adhere.http.AdClient;
import com.haliyoo.adhere.sqlite.DataHelper4Weibo;
import com.haliyoo.adhere.util.BitmapGet;

public class AdUserConstant extends AdClient {
	private static AdUserConstant instance=null;
	public static String user = null;
	public static String pwd = null;
	public static String sign = null;
	public static List<WeiboBean> repostList = null; 
	public static List<WeiboBean> inviteList = null; 
	public static List<WeiboBean> creativeList = null; 
	public static int creativeCount = 0;
	public static int charmCount = 0;
	public static int contributionsCount = 0;
	public static int colleaguesCount = 0;
	
	private AdUserConstant(){};
	public static synchronized AdUserConstant getInstance(){
		if(instance==null)
			instance= new AdUserConstant();
		return instance;
	}

	@Override
	public void exportLoginResponse(long code, String userId, String sign) {
		// TODO Auto-generated method stub
		
		if (code == 1) {
			/// 保存sign
			AdUserConstant.sign = sign;
			AdUserConstant.user = userId;
		}
	}

	@Override
	public void exportLogoutResponse(long code) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void exportPostResponse(long code) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void exportRepostResponse(long code) {
		// TODO Auto-generated method stub
		if (code == 1) {
			/// 清除广告列表
			AdUserConstant.repostList = null;
		}
	}

	@Override
	public void exportListResponse(long code, List<WeiboBean> wbList) {
		// TODO Auto-generated method stub
		if (code == 1) {
			/// 保存广告列表
			AdUserConstant.repostList = wbList;
		}
	}
	
	@Override
	public void exportCountResponse(long code, String customerId, int creativeCount,
			int charmCount, int contributionsCount, int colleaguesCount) {
		// TODO Auto-generated method stub
		if (code == 1) {
			/// 保存
			this.creativeCount = creativeCount;
			this.charmCount = charmCount;
			this.contributionsCount = contributionsCount;
			this.colleaguesCount = colleaguesCount; 
		}
	}
	
	public long publish(Context context, WeiboBean weiboBean) {
		try {
			Weibo weibo = OAuthConstant.getInstance().getWeibo();
			
			weibo.setToken(OAuthConstant.getInstance().getToken(), OAuthConstant.getInstance().getTokenSecret());
			
			/// 获取微博内容
			String strText = weiboBean.getText();
			Status status = null;
			
			/// 发布微博
			if (weiboBean.getExistImage() && weiboBean.getExistLocation()) {
				status = weibo.uploadStatus(strText,
						BitmapGet.convertBitmapToImageItem(weiboBean.getIcon()), weiboBean.getLatitude(),
						weiboBean.getLongitude());
			} else if (weiboBean.getExistImage()) {
				status = weibo.uploadStatus(strText,
						BitmapGet.convertBitmapToImageItem(weiboBean.getIcon()));
			} else if (weiboBean.getExistLocation()) {
				status = weibo.updateStatus(strText, weiboBean.getLatitude(), weiboBean.getLongitude());
			} else {
				status = weibo.updateStatus(strText);
			}
			
			return status.getId();	
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			return -1;
		}
	}

	public boolean repost(Context context, WeiboBean weiboBean, long rawWeiboId, boolean isComment) {
		try {
			Weibo weibo = OAuthConstant.getInstance().getWeibo();
			
			weibo.setToken(OAuthConstant.getInstance().getToken(), OAuthConstant.getInstance().getTokenSecret());
			
			/// 获取微博内容
			String strText = weiboBean.getText();
			
			/// 转发微博
			weibo.repost(String.valueOf(rawWeiboId), strText, isComment? 1: 0);
			
			return true;	
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			return false;
		}
	}

	public boolean comment(Context context, WeiboBean weiboBean, long rawWeiboId) {
		try {
			Weibo weibo = OAuthConstant.getInstance().getWeibo();
			
			weibo.setToken(OAuthConstant.getInstance().getToken(), OAuthConstant.getInstance().getTokenSecret());
			
			/// 获取微博内容
			String strText = weiboBean.getText();
			
			/// 评论微博
			weibo.updateComment(strText, String.valueOf(rawWeiboId), null);
			
			return true;	
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			return false;
		}
	}
	
	/**
	 * 保存微博到数据库
	 * @param weibo 微博内容
	 */
	public static boolean save(Context context, WeiboBean weibo){
    	DataHelper4Weibo dbHelper = null;
    	
        //获取账号列表
        dbHelper = new DataHelper4Weibo(context);
        long id = dbHelper.insert(weibo);
        if (id > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 获取位置的微博文字
	 * @param latitude 纬度
	 * @param longitude 经度
	 * @return 包含经纬度信息的微博
	 */
	public static String getLocation(Context context, double latitude, double longitude) {
		String latLongString = context.getString(R.string.STR_MY_LOC_HEAD)
				+ latitude + "," + longitude + "\n";
		
		return latLongString;
	}
	
	/**
	 * 从xml数据中获取内容（微博来源）
	 * @param source 原始xml数据包
	 * @return 微博来源内容
	 */
	public static String getSourceByXml(String source) {
		int start = source.indexOf(">");
		int end = source.lastIndexOf("</a>");
		
		return source.substring(start + 1, end);
	}
}
